﻿//===============================Stap2Algorithm.cs==============================\\
//===Mark Wallenburg======\\


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Collections;
using System.Drawing.Drawing2D;
using System.IO;

namespace Team2_Vision
{
    class OCRAlgorithm
    {

       // static char curLetter = '0';
        public OCRAlgorithm() { }

        public void LineAlgorithm(Bitmap sourceImage)
        {
            int[][] arrays = new int[6][];
            Image image = new Image(sourceImage, ImageLockMode.ReadWrite);
            byte[] imgArr = image.array;
            byte[] blueChannel = new byte[imgArr.Length / 3];
            int counter = 0;
            for (int i = 0; i < imgArr.Length; i += 3)
            {
                if (imgArr[i] > 60)
                {
                    imgArr[i] = 255;
                }
                else
                {
                    imgArr[i] = 0;
                }
                blueChannel[counter] = imgArr[i];

                counter++;
            }
            arrays[0] = new int[3];
            arrays[1] = new int[3];
            arrays[2] = new int[3];
            arrays[3] = new int[3];
            arrays[4] = new int[3];
            arrays[5] = new int[3];

            arrays[0] = horizontalLine(sourceImage, blueChannel, sourceImage.Width * (sourceImage.Height / 4));
            arrays[1] = horizontalLine(sourceImage, blueChannel, sourceImage.Width*(sourceImage.Height/2));
            arrays[2] = horizontalLine(sourceImage, blueChannel, sourceImage.Width * ((sourceImage.Height / 4)*3));
            arrays[3] = verticalLine(sourceImage, blueChannel, ((sourceImage.Width -1 ) / 4), (sourceImage.Width * sourceImage.Height) - ((sourceImage.Width / 4)*3));
            arrays[4] = verticalLine(sourceImage, blueChannel, ((sourceImage.Width - 1) / 2), (sourceImage.Width * sourceImage.Height) - (sourceImage.Height / 2));
            arrays[5] = verticalLine(sourceImage, blueChannel, (((sourceImage.Width / 4) * 3) - 1), (sourceImage.Width * sourceImage.Height) - (sourceImage.Height / 4));
            for (int i = 0; i < blueChannel.Length; i++ )
            {
                imgArr[i * 3] = blueChannel[i];
                imgArr[i * 3+1] = blueChannel[i];
                imgArr[i * 3+2] = blueChannel[i];
            }

            DecisionTree decie = new DecisionTree();
            decie.InterpretPattern(arrays);

            Bitmap b = image.UnlockToBitmap();

        }


        /// <summary>
        /// Maakt een horizontale lijn
        /// </summary>
        /// <param name="sImg"> Dit moet het input plaatje zijn</param>
        /// <param name="img"> LockedBits array van input plaatje</param>
        /// <returns></returns>
        public int[] horizontalLine(Bitmap sImg, byte[] img, int startY)
        {
            int[] retArray = new int[3] { 0, 0, 0 };
            int pixelPos = startY;
            int pixelVal = 100, arrayLength = 0, surPixEqual = 1;
            bool first = true;
            surPixEqual = 1;
            for (int x = 0; x < sImg.Width; x++)
            {
                if (img[pixelPos + x] != pixelVal)
                {
                    for (int i = 1; i < 8; i++)
                    {
                        if (x < sImg.Width - 8)
                        {
                            if (img[(pixelPos + x) + i] == img[pixelPos + x])
                            {
                                surPixEqual++;
                            }
                        }
                    }
                    if (surPixEqual >= 6)
                    {
                        if (first)
                        {
                            if (img[pixelPos + x] < 50)
                            {
                                retArray[0] = 1;
                            }
                            first = false;
                        }
                        if (img[pixelPos + x] < 50)
                        {
                            retArray[1] = 1;
                        }
                        else
                        {
                            retArray[1] = 0;
                        }
                        arrayLength++;
                        pixelVal = img[pixelPos + x];
                    }
                    surPixEqual = 1;
                }
                img[pixelPos + x] = 100;
            }


            retArray[2] = arrayLength;
            return retArray;
        }
       

        /// <summary>
        /// Maakt een verticale lijn die door het midden van de image gaat
        /// </summary>
        /// <param name="sImg"> Dit moet het input plaatje zijn</param>
        /// <param name="img"> LockedBits array van input plaatje</param>
        /// <returns></returns>
        public int[] verticalLine(Bitmap sImg, byte[] img, int startX, int endX)
        {
            int[] retArray = new int[3] { 0, 0, 0 };
            int pixelPos = startX;
            int pixelVal = 100, arrayLength =0, surPixEqual = 1;
            bool first = true;
            surPixEqual = 1;
            for (int loop = 0; loop < sImg.Height; loop++)
            {
                if (pixelPos < img.Length)
                {
                    if (img[pixelPos] != pixelVal)
                    {
                        for (int y2 = sImg.Width; y2 < sImg.Width * 8; y2 = y2 + sImg.Width)
                        {
                            if (pixelPos < endX && pixelPos + y2 < img.Length)
                            {
                                if (img[(pixelPos) + y2] == img[pixelPos])
                                {
                                    surPixEqual++;
                                }
                            }
                        }
                        if (surPixEqual >= 6)
                        {
                            if (first)
                            {
                                if (img[pixelPos] < 50)
                                {
                                    retArray[0] = 1;
                                }
                                first = false;
                            }
                            if (img[pixelPos] < 50)
                            {
                                retArray[1] = 1;
                            }
                            else
                            {
                                retArray[1] = 0;
                            }
                            arrayLength++;
                            pixelVal = img[pixelPos];
                        }
                        surPixEqual = 1;
                    }
                }
                img[pixelPos] = 100;
                pixelPos = pixelPos + (sImg.Width);
            }
            
            retArray[2] = arrayLength;
            return retArray;
        }
        
        
    }
}